﻿Public Class PrivacyWindowViewModel

    Private _VisibleContacts As Collections.ObjectModel.ObservableCollection(Of ContactViewModel)
    Private _ReadOnlyVisibleContacts As Collections.ObjectModel.ReadOnlyObservableCollection(Of ContactViewModel)

    Private _InvisibleContacts As Collections.ObjectModel.ObservableCollection(Of ContactViewModel)
    Private _ReadOnlyInvisibleContacts As Collections.ObjectModel.ReadOnlyObservableCollection(Of ContactViewModel)

    Private _IgnoredContacts As Collections.ObjectModel.ObservableCollection(Of ContactViewModel)
    Private _ReadOnlyIgnoredContacts As Collections.ObjectModel.ReadOnlyObservableCollection(Of ContactViewModel)

    Private _VisibileContactsBinding As ContactNotifiyingCollectionBinding
    Private _InvisibileContactsBinding As ContactNotifiyingCollectionBinding
    Private _IgnoredContactsBinding As ContactNotifiyingCollectionBinding

    Public Sub New()
        Dim svPrivacy = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IPrivacyService)()

        _VisibleContacts = New Collections.ObjectModel.ObservableCollection(Of ContactViewModel)( _
                                (From x In svPrivacy.VisibleList Select ContactViewModelCache.GetViewModel(x)).ToList)
        _InvisibleContacts = New Collections.ObjectModel.ObservableCollection(Of ContactViewModel)( _
                                (From x In svPrivacy.InvisibleList Select ContactViewModelCache.GetViewModel(x)).ToList)
        _IgnoredContacts = New Collections.ObjectModel.ObservableCollection(Of ContactViewModel)( _
                                (From x In svPrivacy.IgnoreList Select ContactViewModelCache.GetViewModel(x)).ToList)

        _VisibileContactsBinding = New ContactNotifiyingCollectionBinding(svPrivacy.VisibleList, _VisibleContacts)
        _InvisibileContactsBinding = New ContactNotifiyingCollectionBinding(svPrivacy.InvisibleList, _InvisibleContacts)
        _IgnoredContactsBinding = New ContactNotifiyingCollectionBinding(svPrivacy.IgnoreList, _IgnoredContacts)

        _ReadOnlyVisibleContacts = New Collections.ObjectModel.ReadOnlyObservableCollection(Of ContactViewModel)(_VisibleContacts)
        _ReadOnlyInvisibleContacts = New Collections.ObjectModel.ReadOnlyObservableCollection(Of ContactViewModel)(_InvisibleContacts)
        _ReadOnlyIgnoredContacts = New Collections.ObjectModel.ReadOnlyObservableCollection(Of ContactViewModel)(_IgnoredContacts)
    End Sub

    Public ReadOnly Property VisibleContacts() As Collections.ObjectModel.ReadOnlyObservableCollection(Of ContactViewModel)
        Get
            Return _ReadOnlyVisibleContacts
        End Get
    End Property

    Public ReadOnly Property InvisibleContacts() As Collections.ObjectModel.ReadOnlyObservableCollection(Of ContactViewModel)
        Get
            Return _ReadOnlyInvisibleContacts
        End Get
    End Property

    Public ReadOnly Property IgnoredContacts() As Collections.ObjectModel.ReadOnlyObservableCollection(Of ContactViewModel)
        Get
            Return _ReadOnlyIgnoredContacts
        End Get
    End Property

    Public Sub AddInvisibleContact(ByVal identifier As String)
        Dim svPrivacy = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IPrivacyService)()
        Dim svStorage = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IStorageService)()
        Dim contact = svStorage.GetContactByIdentifier(identifier)

        svPrivacy.AddContactToInvisibleList(contact)
    End Sub

    Public Sub RemoveInvisibleContact(ByVal contact As ContactViewModel)
        Dim svPrivacy = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IPrivacyService)()

        svPrivacy.RemoveContactFromInvisibleList(contact.Model)
    End Sub

    Public Sub AddVisibleContact(ByVal identifier As String)
        Dim svPrivacy = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IPrivacyService)()
        Dim svStorage = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IStorageService)()
        Dim contact = svStorage.GetContactByIdentifier(identifier)

        svPrivacy.AddContactToVisibleList(contact)
    End Sub

    Public Sub RemoveVisibleContact(ByVal contact As ContactViewModel)
        Dim svPrivacy = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IPrivacyService)()

        svPrivacy.RemoveContactFromVisibleList(contact.Model)
    End Sub

    Public Sub AddIgnoreContact(ByVal identifier As String)
        Dim svPrivacy = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IPrivacyService)()
        Dim svStorage = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IStorageService)()
        Dim contact = svStorage.GetContactByIdentifier(identifier)

        svPrivacy.AddContactToIgnoreList(contact)
    End Sub

    Public Sub RemoveIgnoreContact(ByVal contact As ContactViewModel)
        Dim svPrivacy = ApplicationService.Current.Context.GetService(Of IcqInterface.Interfaces.IPrivacyService)()

        svPrivacy.RemoveContactFromIgnoreList(contact.Model)
    End Sub

End Class

Public Class ContactNotifiyingCollectionBinding
    Inherits Core.NotifyingCollectionBinding(Of ContactViewModel)

    Public Sub New(ByVal source As Collections.Specialized.INotifyCollectionChanged, ByVal target As Collections.ObjectModel.ObservableCollection(Of ContactViewModel))
        MyBase.New(source, target)
    End Sub

    Protected Overrides Function GetTargetItem(ByVal item As Object) As ContactViewModel
        Dim contact = DirectCast(item, IcqInterface.Interfaces.IContact)

        Return ContactViewModelCache.GetViewModel(contact)
    End Function
End Class